This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 05-28 20:04:43] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 05-28 20:04:43] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 05-28 20:04:43] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-28 20:04:43] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-28 20:04:43] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-28 20:04:43] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-28 20:04:43] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-28 20:04:43] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-28 20:04:43] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 05-28 20:04:43] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 05-28 20:04:43] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.011866, 'x2': 0.934265, 'x3': 0.576401, 'x4': 0.990025, 'x5': 0.045334, 'x6': 0.349471}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.058335, 0.1), 'l2norm': (1.47006, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.391366, 'x2': 0.656668, 'x3': 0.74625, 'x4': 0.251352, 'x5': 0.210257, 'x6': 0.080316}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.773375, 0.1), 'l2norm': (1.273378, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.352689, 'x2': 0.272463, 'x3': 0.992231, 'x4': 0.546235, 'x5': 0.192015, 'x6': 0.748981}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.89699, 0.1), 'l2norm': (1.574447, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.221124, 'x2': 0.037286, 'x3': 0.063618, 'x4': 0.94538, 'x5': 0.730566, 'x6': 0.757034}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (0.141887, 0.1), 'l2norm': (1.538753, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.481573, 'x2': 0.439288, 'x3': 0.734413, 'x4': 0.094791, 'x5': 0.593192, 'x6': 0.720869}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.354329, 0.1), 'l2norm': (1.36331, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.744109, 'x2': 0.240348, 'x3': 0.457357, 'x4': 0.054525, 'x5': 0.847698, 'x6': 0.061584}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (0.05044, 0.1), 'l2norm': (1.277307, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.874101, 'x2': 0.869138, 'x3': 0.341409, 'x4': 0.450336, 'x5': 0.53638, 'x6': 0.802076}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.118197, 0.1), 'l2norm': (1.64493, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.313989, 'x2': 0.264533, 'x3': 0.781376, 'x4': 0.499783, 'x5': 0.801517, 'x6': 0.365405}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.180774, 0.1), 'l2norm': (1.210793, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.758996, 'x2': 0.866141, 'x3': 0.006811, 'x4': 0.663404, 'x5': 0.212611, 'x6': 0.134701}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.219961, 0.1), 'l2norm': (1.243607, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.650952, 'x2': 0.21785, 'x3': 0.127188, 'x4': 0.416127, 'x5': 0.654914, 'x6': 0.46084}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.051037, 0.1), 'l2norm': (1.072612, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.238249, 'x2': 0.432401, 'x3': 0.619579, 'x4': 0.94596, 'x5': 0.12003, 'x6': 0.757662}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.330272, 0.1), 'l2norm': (1.473321, 0.1)}.
[INFO 05-28 20:04:43] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.359882, 'x2': 0.771802, 'x3': 0.063452, 'x4': 0.166276, 'x5': 0.533677, 'x6': 0.138101}.
[INFO 05-28 20:04:43] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.518142, 0.1), 'l2norm': (0.881325, 0.1)}.
[INFO 05-28 20:04:55] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.350413, 'x2': 0.693488, 'x3': 0.463415, 'x4': 0.191461, 'x5': 0.320414, 'x6': 0.135952}.
[INFO 05-28 20:04:55] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.613613, 0.1), 'l2norm': (0.934478, 0.1)}.
[INFO 05-28 20:05:13] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.351146, 'x2': 0.59022, 'x3': 0.612434, 'x4': 0.240411, 'x5': 0.233204, 'x6': 0.183862}.
[INFO 05-28 20:05:13] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-0.65923, 0.1), 'l2norm': (1.233065, 0.1)}.
[INFO 05-28 20:05:26] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.398246, 'x2': 0.780185, 'x3': 0.701751, 'x4': 0.217984, 'x5': 0.296548, 'x6': 0.104318}.
[INFO 05-28 20:05:26] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-0.7491, 0.1), 'l2norm': (1.174832, 0.1)}.
[INFO 05-28 20:05:44] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.415213, 'x2': 0.815073, 'x3': 0.60059, 'x4': 0.125733, 'x5': 0.166816, 'x6': 0.120256}.
[INFO 05-28 20:05:44] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-0.36248, 0.1), 'l2norm': (1.068123, 0.1)}.
[INFO 05-28 20:06:25] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.357686, 'x2': 0.666798, 'x3': 0.687075, 'x4': 0.342001, 'x5': 0.358562, 'x6': 0.126186}.
[INFO 05-28 20:06:25] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-1.288243, 0.1), 'l2norm': (1.397629, 0.1)}.
[INFO 05-28 20:07:08] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.351464, 'x2': 0.702598, 'x3': 0.549054, 'x4': 0.308473, 'x5': 0.399241, 'x6': 0.131338}.
[INFO 05-28 20:07:08] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-1.17262, 0.1), 'l2norm': (1.186949, 0.1)}.
[INFO 05-28 20:08:05] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.350292, 'x2': 0.69977, 'x3': 0.451052, 'x4': 0.36688, 'x5': 0.379274, 'x6': 0.135807}.
[INFO 05-28 20:08:05] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-1.274273, 0.1), 'l2norm': (1.050205, 0.1)}.
[INFO 05-28 20:08:59] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.339528, 'x2': 0.720535, 'x3': 0.520244, 'x4': 0.391221, 'x5': 0.382281, 'x6': 0.093493}.
[INFO 05-28 20:08:59] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-1.598541, 0.1), 'l2norm': (1.344717, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 05-28 20:08:59] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 4 minutes, 34.01 seconds.